export class Coordinate {
  x = 0;
  y = 0;

  constructor(x, y) {
    this.x = x;
    this.y = y;
  }

  update (x, y) {
    this.x = x;
    this.y = y;
  }
}

export class Size {
  top;
  right;
  bottom;
  left;
  initW;
  initH;
  width;
  height;

  constructor($item) {
    this.initW = $item.attr('data-w');
    this.initH = $item.attr('data-h');

    this.width = $item.width()
    this.height = $item.height()

    // console.log($item.attr('data-w'), $item.attr('data-h'))
    if (!this.initW) {
      this.initW = +this.width
      $item.attr('data-w', this.initW)
    }
    if (!this.initH) {
      this.initH = +this.height
      $item.attr('data-h', this.initH)
    }

    // console.log(this.width, this.height, this.initW, this.initH)
    this.update($item)
  }

  update = ($item) => {
    const { top, left } = $item.offset();
    this.top = top;
    this.right = left + this.width;
    this.bottom = top + this.height;
    this.left = left;
  }

  setInitW ($item, w) {
    $item.attr('data-w', w)
    this.initW = +w
  }

  setInitH ($item, h) {
    $item.attr('data-h', h)
    this.initH = +h
  }
}

/**
 *
 * @param size
 * @param max
 * @return {number}
 */
export function sizeForRatio (size, max) {
  return ~~((size / max) * 10000) / 100
}

export function getIntervalTime(time) {
  if (typeof time === "string")  {
    const [start, end] = time.split('_');
    let __start = start.substring(8)
    const __end = end.substring(8)
    if (+__start >= +__end) __start = '000000'
    return [__start, __end]
  } else {
    let { beginTime, endTime } = time
    let __start = dayjs(beginTime).format('HHmmss')
    const __end = dayjs(endTime).format('HHmmss')
    if (+__start >= +__end) __start = '000000'
    return [__start, __end]
  }
}


export function getStartTime(left) {
  const $scroll = $(this.container).find('.time-scroller');
  let width = $scroll.width();  // 获取当前组件的宽度
  const allTime = 1000 * 60 * 60 * 24;
  const start = 1000 * 60 * 60 * 8;
  const scale = allTime / width

  return dayjs(left * scale - start).format('HH:mm:ss')
}

export function createDayHours(size, callback = (arg) => arg) {
  return new Array(size).fill({ text: '00', type: 'hour' }).map(callback)
}



